<template>
    <div>
        <div class="title">我的企业</div>
        <el-table :data="list" class="table" header-row-class-name="table-header">
            <el-table-column fixed prop="name" label="企业名称" align="center" />
            <el-table-column label="认证信息" align="center" width="150">
                <template #default="scope">
                    <img v-if="scope.row.status == 1" src="../../../assets/enterprise/yirenzheng.png">
                    <img v-else src="../../../assets/enterprise/weirenzheng.png">
                </template>
            </el-table-column>
            <el-table-column prop="rolename" label="当前企业身份" align="center" width="150" />
            <el-table-column label="操作" align="center" width="150">
                <template #default="scope">
                    <div v-if="scope.row.status == 1 && scope.row.is_default != 1">
                        <el-button link style="color: #0055a8;" @click="selectCompany(scope.row)">
                            进入企业
                        </el-button>
                    </div>
                    <div v-if="scope.row.status == 0">
                        <el-button link style="color: #0055a8;" @click="goCertify(scope.row)">
                            去认证
                        </el-button>
                        <el-button type="danger" link @click="delItem(scope.row.id)">
                            删除
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script lang="ts" setup>
import { ref, computed, onMounted } from 'vue'
import { CompanyList, DelCompany } from '@/api/enterprise'
import { useCompanyDataStore } from '@/store/companys'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()

const companyDataStore = useCompanyDataStore()

const list = ref([])

const getList = () => {
    CompanyList().then(res => {
        list.value = res.data.filter((item: any) => {
            return item.is_enable == 1
        })
    })
}

const selectCompany = (company: any) => {
    companyDataStore.setDefaultCompany(company)
    if (company.status == 1) {
        router.replace({ name: "home" });
    }
}

const goCertify = (company: any) => {
    companyDataStore.setDefaultCompany(company)
    router.push({ path: '/myEnterprise/certify' })
}

const delItem = (id: number) => {
    DelCompany({
        id: id
    }).then(res => {
        ElMessage.success('删除成功')
        getList()
    })
    getList()
}

onMounted(() => {
    getList()
})
</script>
<style lang="scss" scoped>
.title{
    font-size: 16px;
    margin-bottom: 20px;
}

:deep(.el-table__header) {
    .table-header {
        th {
            font-size: 16px;
            background: #f3f8fb;
            height: 55px;
            color: #6f7070;
            font-weight: normal;
        }
    }
}
</style>